package fr.dauphine.td3.ctl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import fr.dauphine.td3.bdd.EmployeDAO;
import fr.dauphine.td3.emp.Employe;

@Controller
@RequestMapping("user/employes")	
public class EmployesController {
	
	@Autowired
	private EmployeDAO employeDAO;
	
	@RequestMapping(params="validation=Supprimer", method=RequestMethod.POST)
	public String validation (@RequestParam(value="asupprimer", required=false) String[] asupprimer, ModelMap model) {

		if (asupprimer == null)
			return "redirect:user/employes.html";		
		
		int taille = asupprimer.length;
		
		for (int i=0; i < taille; i++) {
			employeDAO.delete(employeDAO.get(asupprimer[i]));			
		}
		
		return "redirect:user/employes.html";
	}
	
	@RequestMapping(params="validation=Ajouter", method=RequestMethod.POST)
	public String ajouter () {
		return "redirect:user/employe.html?id=-1";
	}
	
	@RequestMapping(method=RequestMethod.GET)
	public String affichage( ModelMap model) {
		
		List<Employe> lstEmploye = new ArrayList<Employe>();
		
		try {
			lstEmploye = employeDAO.get();
			
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		
		model.addAttribute("lstEmploye", lstEmploye);
		
		return "employes";
	}	

}
